from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time

a = webdriver.Chrome()
a.get('https://www.qq.com/')
time.sleep(1)

# 获取文章标题
bt_ele = a.find_element(By.CSS_SELECTOR, '[class="news-top"] a')
res2 = bt_ele.text
bt_ele.click()

# 获取所有的窗口列表 [窗口1，窗口2]
all_win = a.window_handles
# 切换窗口 ，根据窗口列表的下标切换
a.switch_to.window(all_win[1])
time.sleep(1)

# 获取正文                                  //正文地址每天都不同
bzz_ele = a.find_element(By.CSS_SELECTOR, '[class="article-wrapper"]')
res3 = bzz_ele.text

# 打开网址
d = webdriver.Chrome()
d.get('http://192.168.0.125:9009/')
time.sleep(1)

# 最大化窗口
d.maximize_window()

# 下拉条
js = "window.scrollTo(0,1000);"
d.execute_script(js)
time.sleep(1)

# 点击登录按钮
dd_ele = d.find_element(By.XPATH, '/html/body/div/div/div/aside/section[6]/ul/li[2]/a')
dd_ele.click()
time.sleep(1)

# 输入账号
y_ele = d.find_element(By.CSS_SELECTOR, 'input[name="log"]')
y_ele.send_keys('admin')
time.sleep(1)

# 输入密码
m_ele = d.find_element(By.CSS_SELECTOR, 'input[name="pwd"]')
m_ele.send_keys('admin123')
time.sleep(1)

# 点击登录按钮
d_ele = d.find_element(By.CSS_SELECTOR, 'input[name="wp-submit"]')
d_ele.click()
time.sleep(1)

# 鼠标在文章上悬停
wz_ele = d.find_element(By.LINK_TEXT, '文章')
ActionChains(d).move_to_element(wz_ele).perform()
time.sleep(1)

# 点击写文章
xwz_ele = d.find_element(By.LINK_TEXT, '写文章')
xwz_ele.click()
time.sleep(1)

# 输入标题
xbbt_ele = d.find_element(By.CSS_SELECTOR, '[name="post_title"]')
xbbt_ele.send_keys(res2)
time.sleep(1)

# 1、先定位到 所处的 frame标签 或者 iframe标签
ele = d.find_element(By.CSS_SELECTOR, '[id="content_ifr"]')
# 2、切换过去 switch_to.frame(表单定位) ，切换过去之后，就只能操作 该表单中的元素了
d.switch_to.frame(ele)
time.sleep(1)

# 输入正文
xnn_ele = d.find_element(By.CSS_SELECTOR, '[id="tinymce"]')
xnn_ele.send_keys(res3)
time.sleep(1)

# 切换表单
d.switch_to.default_content()

# 滚动条
jk = "window.scrollTo(0,-2000);"
d.execute_script(jk)
time.sleep(1)

# 点击发布按钮
fb_ele = d.find_element(By.CSS_SELECTOR, '[name="publish"]')
fb_ele.click()
time.sleep(5)

# 关闭网页
d.quit()
a.quit()
